package edu.ybu.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.mapper.DingMapper;
import edu.ybu.manage.pojo.Ding;
import edu.ybu.manage.pojo.Entor;
import edu.ybu.manage.service.DingService;
import edu.ybu.manage.service.EntorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class DingController {
    @Autowired
    private DingService dingService;


    @GetMapping("/selectAllb")
    public Map<String,Object> selectAll(){
        List<Ding> dings = dingService.list();
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", dings.size());
        res.put("data",dings);
        return  res;
    }
    @GetMapping("/selectByConditionb")
    public Map<String,Object> selectByCondition(String studentnameb){


//    设置条件查询
        LambdaQueryWrapper<Ding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Ding::getId);
        queryWrapper.like(Ding::getStudentnameb,studentnameb);
        List<Ding> dings = dingService.list(queryWrapper);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", dings.size());
        res.put("data",dings);
        return  res;

    }
    /**
     * 增加信息

     * @return
     */
    @GetMapping("/insertStudentb")
    public Integer insertStudent(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Ding ding = null;
        try {
            ding= objectMapper.readValue(json,Ding.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (ding==null){
            return 0;
        }
        return dingService.insert(ding);
    }
    @GetMapping("/deleteStudentb")
    public Integer delectUsers(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Ding ding = null;
        try {
            ding= objectMapper.readValue(json,Ding.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (ding==null){
            return 0;
        }
        return dingService.delect(ding.getId());

    }
    @GetMapping("/updateByIdb")
    public Integer updateById(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Ding ding = null;
        try {
            ding=objectMapper.readValue(json, Ding.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        return dingService.update(ding);
    }





}
